﻿//++
//
// Copyright (c) Microsoft Corporation.  All rights reserved.
//
// Module Name:
//
//    User.cs
//
// Abstract:
//    
//    Contains the channel and rule data for a particular user.
//
//    Note: THIS CODE IS NOT TO BE SHIPPED.
//
//--

using System;
using System.Diagnostics;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using Microsoft.EnhancedReminders;

namespace Microsoft.EnhancedRemindersTools
{
    internal sealed class User
    {
        // Regex to parse a channel description.
        private static Regex _channelRegex = new Regex(@"Channel id=(?<id>[^,]+), enabled=(?<enabled>[^,]+), type=(?<type>(.+)), custom=(?<custom>[^,]+)", RegexOptions.Compiled);

        private readonly Guid _id;
        private List<NotificationChannel> _channels = new List<NotificationChannel>();
        private List<RuleDescription> _rules = new List<RuleDescription>();

        /// <summary>
        /// Ctor.
        /// </summary>
        /// <param name="id">The user ID.</param>
        internal User(Guid id)
        {
            _id = id;
        }

        /// <summary>
        /// Add a channel for this user.  Note that the NotificationChannel type can be used to
        /// encapsulate channel info (some fields such as the userdata are not populated but
        /// this does not matter).
        /// </summary>
        /// <param name="channelInfo">The channel description.</param>
        internal void UpdateChannel(string channelInfo)
        {
            Debug.Assert(!String.IsNullOrEmpty(channelInfo), "channelInfo");

            Match match = _channelRegex.Match(channelInfo);
            if (!match.Success)
            {
                throw new FormatException("Unexpected channel message: " + channelInfo);
            }

            NotificationChannelType channelType = (NotificationChannelType)Enum.Parse(typeof(NotificationChannelType), match.Groups["type"].Value);

            NotificationChannel channel = bool.Parse(match.Groups["custom"].Value) ?
                new NotificationCustomChannel() :
                new NotificationChannel();

            channel.NotificationChannelType = channelType;
            channel.Id = new Guid(match.Groups["id"].Value);
            channel.IsEnabled = match.Groups["enabled"].Value == "true";

            _channels.Add(channel);
        }

        /// <summary>
        /// Add a rule for the current user.
        /// </summary>
        /// <param name="ruleInfo"></param>
        internal void UpdateRule(string ruleInfo)
        {
            _rules.Add(new RuleDescription(ruleInfo));
        }

        /// <summary>
        /// The list of channels.
        /// </summary>
        internal List<NotificationChannel> Channels
        {
            get
            {
                return _channels;
            }
        }

        /// <summary>
        /// The list of rules.
        /// </summary>
        internal List<RuleDescription> Rules
        {
            get
            {
                return _rules;
            }
        }

        /// <summary>
        /// The user ID.
        /// </summary>
        internal Guid Id
        {
            get
            {
                return _id;
            }
        }
    }
}
